<?php

class DiaryTopic
{
    private $apiurl;
    private $apis;
    private $action;
    private $start = 0;
    private $length = 20;
    
    public function __construct($apiurl) {
        $this->apiurl   = $apiurl;
        $this->apis     = array(
            'getDiarysByTopic','editDiaryTopicInfo','searchDiaryTopic',
            'deleteDiaryTopic','getDiaryTopics','getDiaryTopicInfo', 'flushDiaryTopicHotCache','setDiaryRecommend',
            'correctParam','analysisTCUsers','getTopicInfo','autoreply','getParam'
        );
    }
    private function setDiaryRecommend ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        header("Content-type: application/json");
        echo json_encode($res);
        exit;
    }

    public function execute ($action) {
        if (empty($action)) return false;
        if (!in_array($action, $this->apis)) return false;
    
        $this->action = $action;
        $result = $this->$action(request());
        return $result;
    }
    
    private function getDiarysByTopic ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
       
        if(!$req['top']){
        	$pageUrl = $this->pageUrl($req);
        }
        return render('diarys', array('list' => $res, 'pageUrl' => $pageUrl, 'type' => $req['type'], 'sort' => $req['sort'], 'gid'=>$req['gid']));
    }
    
    /**
     * 获取所有点滴话题
     * @param unknown $req
     * @return number
     */
    private function getDiaryTopics ($req) {
        $param = $this->filterArgumentsOfPassed($req);  
        $res   = requestApi($param);

        $pageUrl = $this->pageUrl($req);
        return render('diarytopic', array('list' => $res, 'pageUrl' => $pageUrl, 'type' => $req['type'], 'sort' => $req['sort']));
    }

    /**
     * 话题查询
     * @param unknown $req
     * @return number
     */
    private function correctParam ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $res = $res['topic_info'];
        return render('correctParam', array('list' => $res));
    }

    private function getParam ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $res = json_encode($res['topic_info']);
        return $res;
    }

    /**
     * 话题详情及评论
     * @param unknown $req
     * @return number
     */
    private function getTopicInfo ($req) {
        $req['page'] = $req['page'] ? abs(intval($req['page'])) : 1;

        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);

//        echo "<pre>";print_r($req);die;

        $req['totalPage']  = intval(ceil($req['res_num'] / $this->length));

//        echo "<pre>";print_r($req['totalPage']);die;

        $req['order'] = $param['order'];
        $req['sort'] = $param['sort'];
        $pageUrl = $this->pageUrl($req);

        return render('topic_info', array('list' => $res, 'pageUrl' => $pageUrl));
    }

    /**
     * 话题评论用户分析
     * @param unknown $req
     * @return number
     */
    private function analysisTCUsers ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res   = requestApi($param);
        $res['age']  = !empty($res['age'])    ? $res['age']    : array();
        $res['area'] = !empty($res['area'])   ? $res['area']   : array();
        $res['tag']  = !empty($res['tag'])    ? $res['tag']    : array();
        $res['count']= !empty($res['counts']) ? $res['counts'] : 0;
        $res['data'] = !empty($res['data'])   ? $res['data']   : array();
        return render('analysis_topic_user', array('list' => $res));
    }

    /**
     * 话题自动回复监控
     * @param unknown
     * @return number
     */
    private function autoreply()
    {
        $session   = session_id();
        $adminUid  = $_SESSION['user']['adminUid'];
        $index     = '0';
        return render('autoreply', array('index' => $index ,'adminUid'=>$adminUid ,'session'=>$session ));
    }

    /**
     * 刷新缓存
     * @param unknown $req
     * @return unknown
     */
    private function flushDiaryTopicHotCache ($req) {
        $param = [
            'do'     => 'diaryManage',
            'format' => 'json',
            'operate' => 'delDiaryHotTopicCache',
            'type' => 1
        ];
        $res = requestApi($param);
        echo $res;
    }
    
    /**
     * 获取单个点滴话题信息
     * @param unknown $req
     * @return unknown
     */
    private function deleteDiaryTopic ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
    
        return $res;
    }
    
    /**
     * 删除点滴话题
     * @param unknown $req
     * @return unknown
     */
    private function getDiaryTopicInfo ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        $res = requestApi($param);
        if (empty($res) || empty($res['id'])) return false;

        return render('topicmodal', $res);
    }
    
    /**
     * 编辑点滴话题
     * @param unknown $req
     * @return unknown
     */
    private function editDiaryTopicInfo ($req) {
        $param = $this->filterArgumentsOfPassed($req);

        $res = requestApi($param);
        return $res;
    }
    
    /**
     * 搜索话题
     * @param unknown $req
     * @return unknown
     */
    private function searchDiaryTopic ($req) {
        $param = $this->filterArgumentsOfPassed($req);
        
        $res = requestApi($param);

        return render('diarytopic', array('list' => array($res)));
    }
    
    /**
     * 过滤传参
     * @param unknown $req
     * @return multitype:string unknown Ambigous <number, unknown> Ambigous <string, unknown> NULL number
     */
    private function filterArgumentsOfPassed($req) {
        switch ($this->action){
            case 'getDiarysByTopic':
                $param = array(
                    'do'        => 'getDiarysByTopic',
                    'format'    => 'json',
                    'start'     => empty($req['start']) ? $this->start : $req['start'],
                    'length'	=> empty($req['length']) ? $this->length : $req['length'],
                    'topic_id'  => intval($req['gid']),
                    'top'  	    => empty($req['top']) ? '' : $req['top'],
                    'display'   => empty($req['display'])?0: $req['display']
                );
                break;
            case 'setDiaryRecommend':
                $param = array(
                    'do'        =>  'setDiaryRecommend',
                    'format'    => 'json',
                    'id'        =>  isset($req['bodyId']) ? intval($req['bodyId']) : 0,
                    'sort'      =>  isset($req['sort']) ? intval($req['sort']) : 0,
                    'operate'   => 'recommendEdit'
                );
                break;
            case 'searchDiaryTopic':
                $param = array(
                    'do'     => 'searchDiaryTopic',
                    'format' => 'json',
                    'name'   => trim($req['name'])
                );
                break;
            case 'editDiaryTopicInfo':
                $param = array(
                    'do'     	=> 'editDiaryTopicInfo',
                    'format' 	=> 'json',
                    'id'     	=> intval($req['gid']),
                    'type'   	=> intval($req['type']),
                    'hot'		=> intval($req['hot']),
                    'recommend' => intval($req['recommend']),
                    'img_small' => strval(trim($req['img_small'])),
                    'desc'      => strval(urlencode(trim($req['desc']))),
                    'banner'	=> ''
                );
                
                $banner_img    = strval(trim($req['banner_img']));
                $banner_action = strval(trim($req['banner_action']));
                $banner        = array(
                    array(
                        'id'    =>  $param['id'],
                        'image' =>  $banner_img,
                        'action'=>  $banner_action
                    )
                );
                
                $param['banner'] = JsonFormat($banner);
                break;
            case 'getDiaryTopicInfo':
                $param = array(
                    'do'	   => 'getDiaryTopicInfo',
                    'format'   => 'json',
                    'topic_id' => empty($req['gid']) ? 0: $req['gid']
                );
                break;
            case 'deleteDiaryTopic':
                $param = array(
                    'do'	 => 'deleteDiaryTopic',
                    'format' => 'json',
                    'gid'    => empty($req['gid']) ? 0: $req['gid'],
                    'status' => 3,
                    'type'	 => intval($req['type'])
                );
                break;
            case 'getDiaryTopics':
                $param = array(
                    'do'	 => 'getDiaryTopic',
                    'format' => 'json',
                    'start'  => empty($req['start']) ? $this->start : intval($req['start']),
                    'length' => $this->length,
                    'status' => empty($req['status']) ? 1  : $req['status'],
                    'type'   => 1,
                    'sort'   => empty($req['sort'])   ? '' : $req['sort']
                );
                break;
            case 'correctParam':
                $param = array(
                    'do'	 => 'getAdminTopicInfo',
                    'format' => 'json',
                    'tid'    => isset($req['tid']) ? $req['tid'] : '0',
                );
                break;
            case 'getParam':
                $param = array(
                    'do'	 => 'getAdminTopicInfo',
                    'format' => 'json',
                    'tid'    => isset($req['tid']) ? $req['tid'] : '0',
                );
                break;
            case 'analysisTCUsers':
                $param = array(
                    'do'	 => 'group',
                    'action' => 'getTopicCommentUids',
                    'format' => 'json',
                    'tid'    => isset($req['tid']) ? $req['tid'] : '0',
                );
                break;
            case 'getTopicInfo':
                $param = array(
                    'do'	 => 'getAdminTopicCommentList',
                    'format' => 'json',
                    'tid'    => intval($req['tid']),
                    'author_uid' => intval($req['uid']),
                    'order'  => empty($req['order']) ? 'favorites' : strval($req['order']),
                    'sort'   => empty($req['sort'])  ? 'DESC' : strval($req['sort']),
                    'start'  => (intval($req['page']) - 1) * $this->length,
                    'length' => empty($req['length']) ? $this->length : $req['length'],
                );
                break;
        }
    
        return $param;
    }
    
    /**
     * 分页数据处理
     * @param unknown $req
     * @return multitype:string unknown
     */
    private function pageUrl ($req) {
        $mpurl = "admin.php?ac=".$req['ac']."&op=".$req['op'];
        switch ($req['op']){
            case "getDiaryTopics":
                $mpurl .= "&type={$req['type']}&sort={$req['sort']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
            case "getDiarysByTopic":
                $mpurl .= "&gid={$req['gid']}";
                $pageurl = multi($req['start'], $this->length, $mpurl);
                break;
            case "getTopicInfo":
                $mpurl .= "&tid={$req['tid']}&order={$req['order']}&sort={$req['sort']}";
                $pageurl = multitotal($req['page'], $req['totalPage'], $mpurl);
                break;
        }
        
        return $pageurl;
    }
}

$req = request();
$diaryTopic   = new DiaryTopic($_SC ['apiurl']);
echo $diaryTopic->execute($req['op']);

